package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.KeyBoardUtils;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;


/**
 * 场馆详情界面
 */
public class VenueDetailActivity extends BaseActivity implements View.OnClickListener{
    @ViewInject(R.id.webview)
    WebView webView;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    @ViewInject(R.id.et_comment)
    EditText etComment;
    @ViewInject(R.id.tv_comment)
    TextView tvComment;
    private String id;
    private String local_x;
    private String local_y;
    BaseBean baseBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    @Override
    protected int contentView() {
        return R.layout.activity_venue_detail;
    }

    @Override
    protected void setData() {
        ViewUtils.inject(this);
        id=getIntent().getStringExtra("id");
        local_x=getIntent().getStringExtra("local_x");
        local_y=getIntent().getStringExtra("local_y");
        initToolbar("场地");
        initData();
        llRefresh.setOnClickListener(this);
        tvComment.setOnClickListener(this);
    }

    private void initData() {

        webView.loadUrl(UrlData.IMAGEURL+"/page/site?id=" + id);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                llRefresh.setVisibility(View.VISIBLE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                    startActivity(intent);
                } else if (url.endsWith("location")) {
                    Intent intent = new Intent(VenueDetailActivity.this, BaiduMapActivity.class);
                    intent.putExtra("local_x", local_x);
                    intent.putExtra("local_y", local_y);
                    startActivity(intent);
                }else if(url.startsWith(UrlData.IMAGEURL+"/upload/pic")){
                    Intent intent=new Intent(VenueDetailActivity.this,BigPicActivity.class);
                    intent.putExtra("picUrl",url);
                    startActivity(intent);
                }
                else {
                    webView.loadUrl(url);
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                initData();
                break;
            case R.id.tv_comment:
                venueComment();
                break;
        }
    }

    private void venueComment() {
        if(TextUtils.isEmpty(etComment.getText()))
        {
            showError("请输入评论内容");
        }else{

            MyOkHttpClient okHttpClient=new MyOkHttpClient(this,this);
            okHttpClient.venueComment(UrlData.BASEURL+UrlData.VENUECOMMENT, SharePrefencesUtil.getUser_id(this),id,etComment.getText().toString(),mHandler);
        }
    }
    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try {
                switch (msg.what) {
                    case RESPONSE_SUCCEED:
                        baseBean=gson.fromJson(result,BaseBean.class);
                        if(0==baseBean.getState()){
                            Toast.makeText(VenueDetailActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
                            etComment.setText("");
                            KeyBoardUtils.closeKeybord(etComment, VenueDetailActivity.this);
                            initData();

                        }else{
                           showError(baseBean.getMsg());
                        }
                        break;
                    case RESPONSE_FAILED:
                        showError(result);
                        break;
                }
            } catch (Exception e) {
            }
        }
    };
}
